<template>
    <div>
        <div class="add-btn">
            <el-button type="primary" @click="handleOpen">增加</el-button>
        </div>

        <el-dialog
            v-model="dialogFormVisible"
            :destroy-on-close="true"
            :close-on-click-modal="false"
            title="增加用户"
            @close="handleClose"
        >
            <el-form :model="content">
                <el-form-item label="上传头像" :label-width="formLabelWidth">
                    <el-upload
                        class="avatar-uploader"
                        :action="prefixUrl + '/upload'"
                        :show-file-list="false"
                        :on-success="handleSuccess"
                    >
                        <img v-if="imageUrl" :src="prefixUrl + '/upload/' + imageUrl" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"
                            ><Plus
                        /></el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item label="用户名" :label-width="formLabelWidth">
                    <el-input v-model="studentForm.username" autocomplete="off" />
                </el-form-item>
                <el-form-item label="类别" :label-width="formLabelWidth">
                    <el-select
                        v-model="studentForm.typeId"
                        placeholder="请选择类别"
                        size="large"
                    >
                        <el-option
                            v-for="item in types"
                            :key="item.id"
                            :label="item.info"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogFormVisible = false"
                        >取消</el-button
                    >
                    <el-button type="primary" @click="handleAdd">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import { ElMessage } from "element-plus";
import { defineEmits, ref,reactive } from "vue";
import { Plus } from '@element-plus/icons-vue'
import { storeToRefs } from "pinia";
import { listSystemByTypeId } from "../../../services/system";
import { url } from "../../../services";
import { addUser } from "../../../services/user";

const studentForm = reactive({
    username:"",
    head:"",
    typeId:""
});
const dialogFormVisible = ref(false);
const prefixUrl = ref(url);
const imageUrl = ref("");
const formLabelWidth = ref(100);
const types = ref([]);
const handleOpen = async () => {
    let res = await listSystemByTypeId(2001);
    types.value = res.data;
    dialogFormVisible.value = true;
};
const handleClose = () => {};
const handleAdd = async () => {
    studentForm.head = imageUrl.value;
    await addUser(studentForm);
    dialogFormVisible.value = false;

};
const handleSuccess = (res) => {
    console.log(res);
    imageUrl.value = res;
}
</script>
<style scoped>
.add-btn {
    display: flex;
    justify-content: flex-end;
}
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
.avatar{
    width: 120px;
    height: 120px;
}
</style>
